if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_ACU_ACESSO_USUARIO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_ACU_ACESSO_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_ACU_ACESSO_USUARIO')
          and type = 'TR')
   drop trigger TI_ACU_ACESSO_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_ACU_ACESSO_USUARIO')
          and type = 'TR')
   drop trigger TU_ACU_ACESSO_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_AGC_AGENCIA')
          and type = 'TR')
   drop trigger TD_AGC_AGENCIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_AGC_AGENCIA')
          and type = 'TR')
   drop trigger TI_AGC_AGENCIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_AGC_AGENCIA')
          and type = 'TR')
   drop trigger TU_AGC_AGENCIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_BNC_BANCO')
          and type = 'TR')
   drop trigger TD_BNC_BANCO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_BNC_BANCO')
          and type = 'TR')
   drop trigger TU_BNC_BANCO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CID_CIDADE')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CID_CIDADE
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CID_CIDADE')
          and type = 'TR')
   drop trigger TD_CID_CIDADE
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_CID_CIDADE')
          and type = 'TR')
   drop trigger TI_CID_CIDADE
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CID_CIDADE')
          and type = 'TR')
   drop trigger TU_CID_CIDADE
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CLI_CLIENTE')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CLI_CLIENTE
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CLI_CLIENTE')
          and type = 'TR')
   drop trigger TD_CLI_CLIENTE
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_CLI_CLIENTE')
          and type = 'TR')
   drop trigger TI_CLI_CLIENTE
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CLI_CLIENTE')
          and type = 'TR')
   drop trigger TU_CLI_CLIENTE
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CNB_CONTA_BANCARIA')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CNB_CONTA_BANCARIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CNB_CONTA_BANCARIA')
          and type = 'TR')
   drop trigger TD_CNB_CONTA_BANCARIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_CNB_CONTA_BANCARIA')
          and type = 'TR')
   drop trigger TI_CNB_CONTA_BANCARIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CNB_CONTA_BANCARIA')
          and type = 'TR')
   drop trigger TU_CNB_CONTA_BANCARIA
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CVN_CONVENIO')
          and type = 'TR')
   drop trigger TD_CVN_CONVENIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_CVN_CONVENIO')
          and type = 'TR')
   drop trigger TI_CVN_CONVENIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CVN_CONVENIO')
          and type = 'TR')
   drop trigger TU_CVN_CONVENIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_EMP_EMPRESA')
          and type = 'TR')
   drop trigger TD_EMP_EMPRESA
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_EMP_EMPRESA')
          and type = 'TR')
   drop trigger TU_EMP_EMPRESA
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_END_ENDERECO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_END_ENDERECO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_END_ENDERECO')
          and type = 'TR')
   drop trigger TI_END_ENDERECO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_END_ENDERECO')
          and type = 'TR')
   drop trigger TU_END_ENDERECO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_EST_ESTADO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_EST_ESTADO
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_EST_ESTADO')
          and type = 'TR')
   drop trigger TD_EST_ESTADO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_EST_ESTADO')
          and type = 'TR')
   drop trigger TU_EST_ESTADO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_LCC_LANCAMENTO_CANCELADO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_LCC_LANCAMENTO_CANCELADO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_LCC_LANCAMENTO_CANCELADO')
          and type = 'TR')
   drop trigger TI_LCC_LANCAMENTO_CANCELADO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_LCC_LANCAMENTO_CANCELADO')
          and type = 'TR')
   drop trigger TU_LCC_LANCAMENTO_CANCELADO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_LNC_LANCAMENTO_FINANCEIRO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_LNC_LANCAMENTO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_LNC_LANCAMENTO_FINANCEIRO')
          and type = 'TR')
   drop trigger TI_LNC_LANCAMENTO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_LNC_LANCAMENTO_FINANCEIRO')
          and type = 'TR')
   drop trigger TU_LNC_LANCAMENTO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CLI_CLIENTE3')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CLI_CLIENTE3
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CLI_CLIENTE3')
          and type = 'TR')
   drop trigger TD_CLI_CLIENTE3
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_PEF_PESSOA_FISICA')
          and type = 'TR')
   drop trigger TI_PEF_PESSOA_FISICA
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CLI_CLIENTE3')
          and type = 'TR')
   drop trigger TU_CLI_CLIENTE3
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CLI_CLIENTE4')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CLI_CLIENTE4
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CLI_CLIENTE4')
          and type = 'TR')
   drop trigger TD_CLI_CLIENTE4
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_PEJ_PESSOA_JURIDICA')
          and type = 'TR')
   drop trigger TI_PEJ_PESSOA_JURIDICA
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CLI_CLIENTE4')
          and type = 'TR')
   drop trigger TU_CLI_CLIENTE4
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_CLI_CLIENTE2')
          and type = 'TR')
   drop trigger CLR_TRIGGER_CLI_CLIENTE2
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_CLI_CLIENTE2')
          and type = 'TR')
   drop trigger TD_CLI_CLIENTE2
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_CLI_CLIENTE2')
          and type = 'TR')
   drop trigger TU_CLI_CLIENTE2
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_SAC_SACADO')
          and type = 'TR')
   drop trigger TI_SAC_SACADO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_SAC_SACADO')
          and type = 'TR')
   drop trigger TU_SAC_SACADO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_TEL_TELEFONE')
          and type = 'TR')
   drop trigger CLR_TRIGGER_TEL_TELEFONE
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_TEL_TELEFONE')
          and type = 'TR')
   drop trigger TI_TEL_TELEFONE
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_TEL_TELEFONE')
          and type = 'TR')
   drop trigger TU_TEL_TELEFONE
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_TTF_TITULO_FINANCEIRO')
          and type = 'TR')
   drop trigger TD_TTF_TITULO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_TTF_TITULO_FINANCEIRO')
          and type = 'TR')
   drop trigger TI_TTF_TITULO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_TTF_TITULO_FINANCEIRO')
          and type = 'TR')
   drop trigger TU_TTF_TITULO_FINANCEIRO
go

if exists (select 1
          from sysobjects
          where id = object_id('CLR_TRIGGER_USR_USUARIO')
          and type = 'TR')
   drop trigger CLR_TRIGGER_USR_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TD_USR_USUARIO')
          and type = 'TR')
   drop trigger TD_USR_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TI_USR_USUARIO')
          and type = 'TR')
   drop trigger TI_USR_USUARIO
go

if exists (select 1
          from sysobjects
          where id = object_id('TU_USR_USUARIO')
          and type = 'TR')
   drop trigger TU_USR_USUARIO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ACU_ACESSO_USUARIO')
            and   type = 'U')
   drop table ACU_ACESSO_USUARIO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('AGC_AGENCIA')
            and   type = 'U')
   drop table AGC_AGENCIA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('BNC_BANCO')
            and   type = 'U')
   drop table BNC_BANCO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CID_CIDADE')
            and   type = 'U')
   drop table CID_CIDADE
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CLI_CLIENTE')
            and   type = 'U')
   drop table CLI_CLIENTE
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CNB_CONTA_BANCARIA')
            and   type = 'U')
   drop table CNB_CONTA_BANCARIA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CVN_CONVENIO')
            and   type = 'U')
   drop table CVN_CONVENIO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('EMP_EMPRESA')
            and   type = 'U')
   drop table EMP_EMPRESA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('END_ENDERECO')
            and   type = 'U')
   drop table END_ENDERECO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('EST_ESTADO')
            and   type = 'U')
   drop table EST_ESTADO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('LCC_LANCAMENTO_CANCELADO')
            and   type = 'U')
   drop table LCC_LANCAMENTO_CANCELADO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('LNC_LANCAMENTO_FINANCEIRO')
            and   type = 'U')
   drop table LNC_LANCAMENTO_FINANCEIRO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('PEF_PESSOA_FISICA')
            and   type = 'U')
   drop table PEF_PESSOA_FISICA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('PEJ_PESSOA_JURIDICA')
            and   type = 'U')
   drop table PEJ_PESSOA_JURIDICA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('PES_PESSOA')
            and   type = 'U')
   drop table PES_PESSOA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('SAC_SACADO')
            and   type = 'U')
   drop table SAC_SACADO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('TEL_TELEFONE')
            and   type = 'U')
   drop table TEL_TELEFONE
go

if exists (select 1
            from  sysobjects
           where  id = object_id('TTF_TITULO_FINANCEIRO')
            and   type = 'U')
   drop table TTF_TITULO_FINANCEIRO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('USR_USUARIO')
            and   type = 'U')
   drop table USR_USUARIO
go

/*==============================================================*/
/* Table: ACU_ACESSO_USUARIO                                    */
/*==============================================================*/
create table ACU_ACESSO_USUARIO (
   ACU_ID_ACESSO_USUARIO int                  not null,
   ACU_ID_USUARIO       int                  not null,
   ACU_DT_ACESSO        datetime             not null
)
go

/*==============================================================*/
/* Table: AGC_AGENCIA                                           */
/*==============================================================*/
create table AGC_AGENCIA (
   AGC_ID_AGENCIA       int                  not null,
   AGC_ID_BANCO         int                  not null,
   AGC_NU_AGENCIA       varchar(6)           not null,
   AGC_NU_DIGITO_AGENCIA char(1)              not null,
   AGC_NM_AGENCIA       varchar(50)          not null,
   AGC_NU_CNPJ          char(14)             null
)
go

/*==============================================================*/
/* Table: BNC_BANCO                                             */
/*==============================================================*/
create table BNC_BANCO (
   BNC_ID_BANCO         int                  not null,
   BNC_NU_BANCO         char(3)              not null,
   BNC_NU_DIGITO_BANCO  char(1)              not null,
   BNC_NM_BANCO         varchar(50)          not null,
   BNC_NU_CNPJ          char(14)             not null
)
go

/*==============================================================*/
/* Table: CID_CIDADE                                            */
/*==============================================================*/
create table CID_CIDADE (
   CID_ID_CIDADE        int                  not null,
   CID_ID_ESTADO        int                  not null,
   CID_NM_CIDADE        varchar(100)         not null,
   CID_CD_IBGE          numeric(7)           not null
)
go

/*==============================================================*/
/* Table: CLI_CLIENTE                                           */
/*==============================================================*/
create table CLI_CLIENTE (
   CLI_ID_CLIENTE       int                  not null,
   CLI_ID_PESSOA        int                  not null,
   CLI_CH_ATIVO         bit                  not null
)
go

/*==============================================================*/
/* Table: CNB_CONTA_BANCARIA                                    */
/*==============================================================*/
create table CNB_CONTA_BANCARIA (
   CNB_ID_CONTA         int                  not null,
   CNB_TP_PROPRIETARIO  char(1)              not null,
   CNB_ID_CLIENTE       int                  null,
   CNB_ID_EMPRESA       int                  null,
   CNB_ID_AGENCIA       int                  not null,
   CNB_DS_CONTA         varchar(50)          not null,
   CNB_NU_CONTA         varchar(25)          not null,
   CNB_NU_DIGITO_CONTA  varchar(1)           not null,
   CNB_VL_SALDO_INICIAL money	           not null,
   CNB_CD_VARIACAO      char(2)              null,
   CNB_DT_ABERTURA      datetime             not null
)
go

/*==============================================================*/
/* Table: CVN_CONVENIO                                          */
/*==============================================================*/
create table CVN_CONVENIO (
   CVN_ID_CONVENIO      int                  not null,
   CNB_ID_CONTA         int                  not null,
   CVN_CD_CEDENTE       char(8)              not null,
   CVN_NU_PROXIMO_NOSSO_NUMERO char(20)             not null,
   CVN_CD_VARIACAO      char(3)              null
)
go

/*==============================================================*/
/* Table: EMP_EMPRESA                                           */
/*==============================================================*/
create table EMP_EMPRESA (
   EMP_ID_EMPRESA       int                  not null,
   EMP_NM_EMPRESA       varchar(50)          not null,
   EMP_NU_CNPJ          char(14)             not null
)
go

/*==============================================================*/
/* Table: END_ENDERECO                                          */
/*==============================================================*/
create table END_ENDERECO (
   END_ID_ENDERECO      int                  not null,
   END_ID_CIDADE        int                  not null,
   END_ID_ESTADO        int                  not null,
   END_ID_PESSOA        int                  not null,
   END_NM_LOGRADOURO    varchar(200)         not null,
   END_DS_COMPLEMENTO   varchar(100)         not null,
   END_NU_IMOVEL        varchar(6)           null,
   END_NM_BAIRRO        varchar(50)          null,
   END_NU_CEP           varchar(8)           not null
)
go

/*==============================================================*/
/* Table: EST_ESTADO                                            */
/*==============================================================*/
create table EST_ESTADO (
   EST_ID_ESTADO        int                  not null,
   EST_SG_ESTADO        varchar(2)           not null,
   EST_NM_ESTADO        varchar(50)          not null,
   EST_CD_IBGE          numeric(2)           not null
)
go

/*==============================================================*/
/* Table: LCC_LANCAMENTO_CANCELADO                              */
/*==============================================================*/
create table LCC_LANCAMENTO_CANCELADO (
   LCC_ID_LANCAMENTO    int                  not null,
   LCC_ID_CONTA         int                  not null,
   LCC_ID_USUARIO_LANCAMENTO int                  not null,
   LCC_ID_USUARIO_CANCELAMENTO int                  not null,
   LCC_DT_LANCAMENTO    datetime             not null,
   LCC_DT_REGISTRO      datetime             not null,
   LCC_VL_LANCAMENTO    money           not null,
   LCC_TP_LANCAMENTO    varchar(1)           not null,
   LCC_TP_CREDITO_DEBITO varchar(1)           not null,
   LCC_DT_CANCELAMENTO  datetime             not null
)
go

/*==============================================================*/
/* Table: LNC_LANCAMENTO_FINANCEIRO                             */
/*==============================================================*/
create table LNC_LANCAMENTO_FINANCEIRO (
   LNC_ID_LANCAMENTO    int                  not null,
   LNC_ID_CONTA         int                  not null,
   LNC_ID_TITULO        int                  null,
   LNC_DT_LANCAMENTO    datetime             not null,
   LNC_DT_REGISTRO      datetime             not null,
   LNC_VL_LANCAMENTO    money	           not null,
   LNC_TP_LANCAMENTO    varchar(1)           not null,
   LNC_TP_CREDITO_DEBITO varchar(1)           not null
)
go

/*==============================================================*/
/* Table: PEF_PESSOA_FISICA                                     */
/*==============================================================*/
create table PEF_PESSOA_FISICA (
   PEF_ID_PESSOA_FISICA int                  not null,
   PEF_ID_PESSOA        int                  not null
)
go

/*==============================================================*/
/* Table: PEJ_PESSOA_JURIDICA                                   */
/*==============================================================*/
create table PEJ_PESSOA_JURIDICA (
   PEJ_ID_PESSOA_JURIDICA int                  not null,
   PEJ_ID_PESSOA        int                  not null,
   PEJ_NM_RAZAO_SOCIAL  varchar(100)         not null,
   PEJ_NU_INSCRICAO_ESTADUAL varchar(25)          null,
   PEJ_NU_INSCRICAO_MUNICIPAL varchar(25)          null
)
go

/*==============================================================*/
/* Table: PES_PESSOA                                            */
/*==============================================================*/
create table PES_PESSOA (
   PES_ID_PESSOA        int                  not null,
   PES_NM_PESSOA        varchar(100)         not null,
   PES_NU_CPF_CNPJ      varchar(14)          not null,
   PES_DT_CADASTRO      datetime             not null
)
go

/*==============================================================*/
/* Table: SAC_SACADO                                            */
/*==============================================================*/
create table SAC_SACADO (
   SAC_ID_SACADO        int                  not null,
   SAC_ID_PESSOA        int                  not null
)
go

/*==============================================================*/
/* Table: TEL_TELEFONE                                          */
/*==============================================================*/
create table TEL_TELEFONE (
   TEL_ID_TELEFONE      int                  not null,
   TEL_ID_PESSOA        int                  not null,
   TEL_CD_DDD           varchar(4)           not null,
   TEL_NU_TELEFONE      varchar(11)          not null
)
go

/*==============================================================*/
/* Table: TTF_TITULO_FINANCEIRO                                 */
/*==============================================================*/
create table TTF_TITULO_FINANCEIRO (
   TTF_ID_TITULO        int                  not null,
   TTF_ID_CONVENIO      int                  null,
   TTF_ID_CLIENTE       int                  not null,
   TTF_ID_SACADO        int                  not null,
   TTF_VL_TITULO        money           not null,
   TTF_NU_NOSSO_NUMERO  char(20)             null,
   TTF_DT_EMISSAO_BOLETO datetime             null,
   TTF_DT_VENCIMENTO    datetime             not null,
   TTF_DT_CADASTRO      datetime             not null,
   TTF_NU_TITULO        int                  not null
)
go

/*==============================================================*/
/* Table: USR_USUARIO                                           */
/*==============================================================*/
create table USR_USUARIO (
   USR_ID_USUARIO       int                  not null,
   USR_ID_PESSOA        int                  null,
   USR_ID_CLIENTE       int                  null,
   USR_TP_USUARIO       int                  not null,
   USR_DS_NICKNAME      varchar(50)          not null,
   USR_DS_EMAIL         varchar(100)         not null,
   USR_DS_SENHA         varchar(25)          not null,
   USR_DS_NOME          varchar(50)          not null,
   USR_DS_SOBRENOME     varchar(150)         not null
)
go


create trigger TI_ACU_ACESSO_USUARIO on ACU_ACESSO_USUARIO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "USR_USUARIO" must exist when inserting a child in "ACU_ACESSO_USUARIO"  */
    if update(ACU_ID_USUARIO)
    begin
       if (select count(*)
           from   USR_USUARIO t1, inserted t2
           where  t1.USR_ID_USUARIO = t2.ACU_ID_USUARIO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "USR_USUARIO". Cannot create child in "ACU_ACESSO_USUARIO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_ACU_ACESSO_USUARIO on ACU_ACESSO_USUARIO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "USR_USUARIO" must exist when updating a child in "ACU_ACESSO_USUARIO"  */
      if update(ACU_ID_USUARIO)
      begin
         if (select count(*)
             from   USR_USUARIO t1, inserted t2
             where  t1.USR_ID_USUARIO = t2.ACU_ID_USUARIO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'USR_USUARIO" does not exist. Cannot modify child in "ACU_ACESSO_USUARIO".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_AGC_AGENCIA on AGC_AGENCIA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "AGC_AGENCIA" if children still exist in "CNB_CONTA_BANCARIA"  */
    if exists (select 1
               from   CNB_CONTA_BANCARIA t2, deleted t1
               where  t2.CNB_ID_AGENCIA = t1.AGC_ID_AGENCIA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot delete parent "AGC_AGENCIA".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_AGC_AGENCIA on AGC_AGENCIA for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "BNC_BANCO" must exist when inserting a child in "AGC_AGENCIA"  */
    if update(AGC_ID_BANCO)
    begin
       if (select count(*)
           from   BNC_BANCO t1, inserted t2
           where  t1.BNC_ID_BANCO = t2.AGC_ID_BANCO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "BNC_BANCO". Cannot create child in "AGC_AGENCIA".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_AGC_AGENCIA on AGC_AGENCIA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "BNC_BANCO" must exist when updating a child in "AGC_AGENCIA"  */
      if update(AGC_ID_BANCO)
      begin
         if (select count(*)
             from   BNC_BANCO t1, inserted t2
             where  t1.BNC_ID_BANCO = t2.AGC_ID_BANCO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'BNC_BANCO" does not exist. Cannot modify child in "AGC_AGENCIA".'
               goto error
            end
      end
      /*  Cannot modify parent code in "AGC_AGENCIA" if children still exist in "CNB_CONTA_BANCARIA"  */
      if update(AGC_ID_AGENCIA)
      begin
         if exists (select 1
                    from   CNB_CONTA_BANCARIA t2, inserted i1, deleted d1
                    where  t2.CNB_ID_AGENCIA = d1.AGC_ID_AGENCIA
                     and  (i1.AGC_ID_AGENCIA != d1.AGC_ID_AGENCIA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot modify parent code in "AGC_AGENCIA".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_BNC_BANCO on BNC_BANCO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "BNC_BANCO" if children still exist in "AGC_AGENCIA"  */
    if exists (select 1
               from   AGC_AGENCIA t2, deleted t1
               where  t2.AGC_ID_BANCO = t1.BNC_ID_BANCO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "AGC_AGENCIA". Cannot delete parent "BNC_BANCO".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_BNC_BANCO on BNC_BANCO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Cannot modify parent code in "BNC_BANCO" if children still exist in "AGC_AGENCIA"  */
      if update(BNC_ID_BANCO)
      begin
         if exists (select 1
                    from   AGC_AGENCIA t2, inserted i1, deleted d1
                    where  t2.AGC_ID_BANCO = d1.BNC_ID_BANCO
                     and  (i1.BNC_ID_BANCO != d1.BNC_ID_BANCO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "AGC_AGENCIA". Cannot modify parent code in "BNC_BANCO".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CID_CIDADE on CID_CIDADE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "CID_CIDADE" if children still exist in "END_ENDERECO"  */
    if exists (select 1
               from   END_ENDERECO t2, deleted t1
               where  t2.END_ID_CIDADE = t1.CID_ID_CIDADE)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "END_ENDERECO". Cannot delete parent "CID_CIDADE".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_CID_CIDADE on CID_CIDADE for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "EST_ESTADO" must exist when inserting a child in "CID_CIDADE"  */
    if update(CID_ID_ESTADO)
    begin
       if (select count(*)
           from   EST_ESTADO t1, inserted t2
           where  t1.EST_ID_ESTADO = t2.CID_ID_ESTADO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "EST_ESTADO". Cannot create child in "CID_CIDADE".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CID_CIDADE on CID_CIDADE for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "EST_ESTADO" must exist when updating a child in "CID_CIDADE"  */
      if update(CID_ID_ESTADO)
      begin
         if (select count(*)
             from   EST_ESTADO t1, inserted t2
             where  t1.EST_ID_ESTADO = t2.CID_ID_ESTADO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'EST_ESTADO" does not exist. Cannot modify child in "CID_CIDADE".'
               goto error
            end
      end
      /*  Cannot modify parent code in "CID_CIDADE" if children still exist in "END_ENDERECO"  */
      if update(CID_ID_CIDADE)
      begin
         if exists (select 1
                    from   END_ENDERECO t2, inserted i1, deleted d1
                    where  t2.END_ID_CIDADE = d1.CID_ID_CIDADE
                     and  (i1.CID_ID_CIDADE != d1.CID_ID_CIDADE))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "END_ENDERECO". Cannot modify parent code in "CID_CIDADE".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CLI_CLIENTE on CLI_CLIENTE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "CLI_CLIENTE" if children still exist in "CNB_CONTA_BANCARIA"  */
    if exists (select 1
               from   CNB_CONTA_BANCARIA t2, deleted t1
               where  t2.CNB_ID_CLIENTE = t1.CLI_ID_CLIENTE)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot delete parent "CLI_CLIENTE".'
          goto error
       end

    /*  Cannot delete parent "CLI_CLIENTE" if children still exist in "TTF_TITULO_FINANCEIRO"  */
    if exists (select 1
               from   TTF_TITULO_FINANCEIRO t2, deleted t1
               where  t2.TTF_ID_CLIENTE = t1.CLI_ID_CLIENTE)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "TTF_TITULO_FINANCEIRO". Cannot delete parent "CLI_CLIENTE".'
          goto error
       end

    /*  Cannot delete parent "CLI_CLIENTE" if children still exist in "USR_USUARIO"  */
    if exists (select 1
               from   USR_USUARIO t2, deleted t1
               where  t2.USR_ID_CLIENTE = t1.CLI_ID_CLIENTE)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "USR_USUARIO". Cannot delete parent "CLI_CLIENTE".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_CLI_CLIENTE on CLI_CLIENTE for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "CLI_CLIENTE"  */
    if update(CLI_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.CLI_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "CLI_CLIENTE".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CLI_CLIENTE on CLI_CLIENTE for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "PES_PESSOA" must exist when updating a child in "CLI_CLIENTE"  */
      if update(CLI_ID_PESSOA)
      begin
         if (select count(*)
             from   PES_PESSOA t1, inserted t2
             where  t1.PES_ID_PESSOA = t2.CLI_ID_PESSOA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'PES_PESSOA" does not exist. Cannot modify child in "CLI_CLIENTE".'
               goto error
            end
      end
      /*  Cannot modify parent code in "CLI_CLIENTE" if children still exist in "CNB_CONTA_BANCARIA"  */
      if update(CLI_ID_CLIENTE)
      begin
         if exists (select 1
                    from   CNB_CONTA_BANCARIA t2, inserted i1, deleted d1
                    where  t2.CNB_ID_CLIENTE = d1.CLI_ID_CLIENTE
                     and  (i1.CLI_ID_CLIENTE != d1.CLI_ID_CLIENTE))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot modify parent code in "CLI_CLIENTE".'
               goto error
            end
      end

      /*  Cannot modify parent code in "CLI_CLIENTE" if children still exist in "TTF_TITULO_FINANCEIRO"  */
      if update(CLI_ID_CLIENTE)
      begin
         if exists (select 1
                    from   TTF_TITULO_FINANCEIRO t2, inserted i1, deleted d1
                    where  t2.TTF_ID_CLIENTE = d1.CLI_ID_CLIENTE
                     and  (i1.CLI_ID_CLIENTE != d1.CLI_ID_CLIENTE))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "TTF_TITULO_FINANCEIRO". Cannot modify parent code in "CLI_CLIENTE".'
               goto error
            end
      end

      /*  Cannot modify parent code in "CLI_CLIENTE" if children still exist in "USR_USUARIO"  */
      if update(CLI_ID_CLIENTE)
      begin
         if exists (select 1
                    from   USR_USUARIO t2, inserted i1, deleted d1
                    where  t2.USR_ID_CLIENTE = d1.CLI_ID_CLIENTE
                     and  (i1.CLI_ID_CLIENTE != d1.CLI_ID_CLIENTE))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "USR_USUARIO". Cannot modify parent code in "CLI_CLIENTE".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CNB_CONTA_BANCARIA on CNB_CONTA_BANCARIA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "CNB_CONTA_BANCARIA" if children still exist in "LNC_LANCAMENTO_FINANCEIRO"  */
    if exists (select 1
               from   LNC_LANCAMENTO_FINANCEIRO t2, deleted t1
               where  t2.LNC_ID_CONTA = t1.CNB_ID_CONTA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "LNC_LANCAMENTO_FINANCEIRO". Cannot delete parent "CNB_CONTA_BANCARIA".'
          goto error
       end

    /*  Cannot delete parent "CNB_CONTA_BANCARIA" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
    if exists (select 1
               from   LCC_LANCAMENTO_CANCELADO t2, deleted t1
               where  t2.LCC_ID_CONTA = t1.CNB_ID_CONTA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot delete parent "CNB_CONTA_BANCARIA".'
          goto error
       end

    /*  Cannot delete parent "CNB_CONTA_BANCARIA" if children still exist in "CVN_CONVENIO"  */
    if exists (select 1
               from   CVN_CONVENIO t2, deleted t1
               where  t2.CNB_ID_CONTA = t1.CNB_ID_CONTA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CVN_CONVENIO". Cannot delete parent "CNB_CONTA_BANCARIA".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_CNB_CONTA_BANCARIA on CNB_CONTA_BANCARIA for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "AGC_AGENCIA" must exist when inserting a child in "CNB_CONTA_BANCARIA"  */
    if update(CNB_ID_AGENCIA)
    begin
       if (select count(*)
           from   AGC_AGENCIA t1, inserted t2
           where  t1.AGC_ID_AGENCIA = t2.CNB_ID_AGENCIA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "AGC_AGENCIA". Cannot create child in "CNB_CONTA_BANCARIA".'
             goto error
          end
    end
    /*  Parent "CLI_CLIENTE" must exist when inserting a child in "CNB_CONTA_BANCARIA"  */
    if update(CNB_ID_CLIENTE)
    begin
       select @numnull = (select count(*)
                          from   inserted
                          where  CNB_ID_CLIENTE is null)
       if @numnull != @numrows
          if (select count(*)
              from   CLI_CLIENTE t1, inserted t2
              where  t1.CLI_ID_CLIENTE = t2.CNB_ID_CLIENTE) != @numrows - @numnull
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CLI_CLIENTE". Cannot create child in "CNB_CONTA_BANCARIA".'
             goto error
          end
    end
    /*  Parent "EMP_EMPRESA" must exist when inserting a child in "CNB_CONTA_BANCARIA"  */
    if update(CNB_ID_EMPRESA)
    begin
       select @numnull = (select count(*)
                          from   inserted
                          where  CNB_ID_EMPRESA is null)
       if @numnull != @numrows
          if (select count(*)
              from   EMP_EMPRESA t1, inserted t2
              where  t1.EMP_ID_EMPRESA = t2.CNB_ID_EMPRESA) != @numrows - @numnull
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "EMP_EMPRESA". Cannot create child in "CNB_CONTA_BANCARIA".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CNB_CONTA_BANCARIA on CNB_CONTA_BANCARIA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "AGC_AGENCIA" must exist when updating a child in "CNB_CONTA_BANCARIA"  */
      if update(CNB_ID_AGENCIA)
      begin
         if (select count(*)
             from   AGC_AGENCIA t1, inserted t2
             where  t1.AGC_ID_AGENCIA = t2.CNB_ID_AGENCIA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'AGC_AGENCIA" does not exist. Cannot modify child in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end
      /*  Parent "CLI_CLIENTE" must exist when updating a child in "CNB_CONTA_BANCARIA"  */
      if update(CNB_ID_CLIENTE)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  CNB_ID_CLIENTE is null)
         if @numnull != @numrows
            if (select count(*)
                from   CLI_CLIENTE t1, inserted t2
                where  t1.CLI_ID_CLIENTE = t2.CNB_ID_CLIENTE) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'CLI_CLIENTE" does not exist. Cannot modify child in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end
      /*  Parent "EMP_EMPRESA" must exist when updating a child in "CNB_CONTA_BANCARIA"  */
      if update(CNB_ID_EMPRESA)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  CNB_ID_EMPRESA is null)
         if @numnull != @numrows
            if (select count(*)
                from   EMP_EMPRESA t1, inserted t2
                where  t1.EMP_ID_EMPRESA = t2.CNB_ID_EMPRESA) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'EMP_EMPRESA" does not exist. Cannot modify child in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end
      /*  Cannot modify parent code in "CNB_CONTA_BANCARIA" if children still exist in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(CNB_ID_CONTA)
      begin
         if exists (select 1
                    from   LNC_LANCAMENTO_FINANCEIRO t2, inserted i1, deleted d1
                    where  t2.LNC_ID_CONTA = d1.CNB_ID_CONTA
                     and  (i1.CNB_ID_CONTA != d1.CNB_ID_CONTA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "LNC_LANCAMENTO_FINANCEIRO". Cannot modify parent code in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "CNB_CONTA_BANCARIA" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
      if update(CNB_ID_CONTA)
      begin
         if exists (select 1
                    from   LCC_LANCAMENTO_CANCELADO t2, inserted i1, deleted d1
                    where  t2.LCC_ID_CONTA = d1.CNB_ID_CONTA
                     and  (i1.CNB_ID_CONTA != d1.CNB_ID_CONTA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot modify parent code in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "CNB_CONTA_BANCARIA" if children still exist in "CVN_CONVENIO"  */
      if update(CNB_ID_CONTA)
      begin
         if exists (select 1
                    from   CVN_CONVENIO t2, inserted i1, deleted d1
                    where  t2.CNB_ID_CONTA = d1.CNB_ID_CONTA
                     and  (i1.CNB_ID_CONTA != d1.CNB_ID_CONTA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CVN_CONVENIO". Cannot modify parent code in "CNB_CONTA_BANCARIA".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CVN_CONVENIO on CVN_CONVENIO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "CVN_CONVENIO" if children still exist in "TTF_TITULO_FINANCEIRO"  */
    if exists (select 1
               from   TTF_TITULO_FINANCEIRO t2, deleted t1
               where  t2.TTF_ID_CONVENIO = t1.CVN_ID_CONVENIO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "TTF_TITULO_FINANCEIRO". Cannot delete parent "CVN_CONVENIO".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_CVN_CONVENIO on CVN_CONVENIO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "CNB_CONTA_BANCARIA" must exist when inserting a child in "CVN_CONVENIO"  */
    if update(CNB_ID_CONTA)
    begin
       if (select count(*)
           from   CNB_CONTA_BANCARIA t1, inserted t2
           where  t1.CNB_ID_CONTA = t2.CNB_ID_CONTA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CNB_CONTA_BANCARIA". Cannot create child in "CVN_CONVENIO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CVN_CONVENIO on CVN_CONVENIO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "CNB_CONTA_BANCARIA" must exist when updating a child in "CVN_CONVENIO"  */
      if update(CNB_ID_CONTA)
      begin
         if (select count(*)
             from   CNB_CONTA_BANCARIA t1, inserted t2
             where  t1.CNB_ID_CONTA = t2.CNB_ID_CONTA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CNB_CONTA_BANCARIA" does not exist. Cannot modify child in "CVN_CONVENIO".'
               goto error
            end
      end
      /*  Cannot modify parent code in "CVN_CONVENIO" if children still exist in "TTF_TITULO_FINANCEIRO"  */
      if update(CVN_ID_CONVENIO)
      begin
         if exists (select 1
                    from   TTF_TITULO_FINANCEIRO t2, inserted i1, deleted d1
                    where  t2.TTF_ID_CONVENIO = d1.CVN_ID_CONVENIO
                     and  (i1.CVN_ID_CONVENIO != d1.CVN_ID_CONVENIO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "TTF_TITULO_FINANCEIRO". Cannot modify parent code in "CVN_CONVENIO".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_EMP_EMPRESA on EMP_EMPRESA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "EMP_EMPRESA" if children still exist in "CNB_CONTA_BANCARIA"  */
    if exists (select 1
               from   CNB_CONTA_BANCARIA t2, deleted t1
               where  t2.CNB_ID_EMPRESA = t1.EMP_ID_EMPRESA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot delete parent "EMP_EMPRESA".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_EMP_EMPRESA on EMP_EMPRESA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Cannot modify parent code in "EMP_EMPRESA" if children still exist in "CNB_CONTA_BANCARIA"  */
      if update(EMP_ID_EMPRESA)
      begin
         if exists (select 1
                    from   CNB_CONTA_BANCARIA t2, inserted i1, deleted d1
                    where  t2.CNB_ID_EMPRESA = d1.EMP_ID_EMPRESA
                     and  (i1.EMP_ID_EMPRESA != d1.EMP_ID_EMPRESA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CNB_CONTA_BANCARIA". Cannot modify parent code in "EMP_EMPRESA".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_END_ENDERECO on END_ENDERECO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "CID_CIDADE" must exist when inserting a child in "END_ENDERECO"  */
    if update(END_ID_CIDADE)
    begin
       if (select count(*)
           from   CID_CIDADE t1, inserted t2
           where  t1.CID_ID_CIDADE = t2.END_ID_CIDADE) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CID_CIDADE". Cannot create child in "END_ENDERECO".'
             goto error
          end
    end
    /*  Parent "PES_PESSOA" must exist when inserting a child in "END_ENDERECO"  */
    if update(END_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.END_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "END_ENDERECO".'
             goto error
          end
    end
    /*  Parent "EST_ESTADO" must exist when inserting a child in "END_ENDERECO"  */
    if update(END_ID_ESTADO)
    begin
       if (select count(*)
           from   EST_ESTADO t1, inserted t2
           where  t1.EST_ID_ESTADO = t2.END_ID_ESTADO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "EST_ESTADO". Cannot create child in "END_ENDERECO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_END_ENDERECO on END_ENDERECO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "CID_CIDADE" must exist when updating a child in "END_ENDERECO"  */
      if update(END_ID_CIDADE)
      begin
         if (select count(*)
             from   CID_CIDADE t1, inserted t2
             where  t1.CID_ID_CIDADE = t2.END_ID_CIDADE) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CID_CIDADE" does not exist. Cannot modify child in "END_ENDERECO".'
               goto error
            end
      end
      /*  Parent "PES_PESSOA" must exist when updating a child in "END_ENDERECO"  */
      if update(END_ID_PESSOA)
      begin
         if (select count(*)
             from   PES_PESSOA t1, inserted t2
             where  t1.PES_ID_PESSOA = t2.END_ID_PESSOA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'PES_PESSOA" does not exist. Cannot modify child in "END_ENDERECO".'
               goto error
            end
      end
      /*  Parent "EST_ESTADO" must exist when updating a child in "END_ENDERECO"  */
      if update(END_ID_ESTADO)
      begin
         if (select count(*)
             from   EST_ESTADO t1, inserted t2
             where  t1.EST_ID_ESTADO = t2.END_ID_ESTADO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'EST_ESTADO" does not exist. Cannot modify child in "END_ENDERECO".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_EST_ESTADO on EST_ESTADO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "EST_ESTADO" if children still exist in "CID_CIDADE"  */
    if exists (select 1
               from   CID_CIDADE t2, deleted t1
               where  t2.CID_ID_ESTADO = t1.EST_ID_ESTADO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CID_CIDADE". Cannot delete parent "EST_ESTADO".'
          goto error
       end

    /*  Cannot delete parent "EST_ESTADO" if children still exist in "END_ENDERECO"  */
    if exists (select 1
               from   END_ENDERECO t2, deleted t1
               where  t2.END_ID_ESTADO = t1.EST_ID_ESTADO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "END_ENDERECO". Cannot delete parent "EST_ESTADO".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_EST_ESTADO on EST_ESTADO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Cannot modify parent code in "EST_ESTADO" if children still exist in "CID_CIDADE"  */
      if update(EST_ID_ESTADO)
      begin
         if exists (select 1
                    from   CID_CIDADE t2, inserted i1, deleted d1
                    where  t2.CID_ID_ESTADO = d1.EST_ID_ESTADO
                     and  (i1.EST_ID_ESTADO != d1.EST_ID_ESTADO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CID_CIDADE". Cannot modify parent code in "EST_ESTADO".'
               goto error
            end
      end

      /*  Cannot modify parent code in "EST_ESTADO" if children still exist in "END_ENDERECO"  */
      if update(EST_ID_ESTADO)
      begin
         if exists (select 1
                    from   END_ENDERECO t2, inserted i1, deleted d1
                    where  t2.END_ID_ESTADO = d1.EST_ID_ESTADO
                     and  (i1.EST_ID_ESTADO != d1.EST_ID_ESTADO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "END_ENDERECO". Cannot modify parent code in "EST_ESTADO".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_LCC_LANCAMENTO_CANCELADO on LCC_LANCAMENTO_CANCELADO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "CNB_CONTA_BANCARIA" must exist when inserting a child in "LCC_LANCAMENTO_CANCELADO"  */
    if update(LCC_ID_CONTA)
    begin
       if (select count(*)
           from   CNB_CONTA_BANCARIA t1, inserted t2
           where  t1.CNB_ID_CONTA = t2.LCC_ID_CONTA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CNB_CONTA_BANCARIA". Cannot create child in "LCC_LANCAMENTO_CANCELADO".'
             goto error
          end
    end
    /*  Parent "USR_USUARIO" must exist when inserting a child in "LCC_LANCAMENTO_CANCELADO"  */
    if update(LCC_ID_USUARIO_LANCAMENTO)
    begin
       if (select count(*)
           from   USR_USUARIO t1, inserted t2
           where  t1.USR_ID_USUARIO = t2.LCC_ID_USUARIO_LANCAMENTO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "USR_USUARIO". Cannot create child in "LCC_LANCAMENTO_CANCELADO".'
             goto error
          end
    end
    /*  Parent "USR_USUARIO" must exist when inserting a child in "LCC_LANCAMENTO_CANCELADO"  */
    if update(LCC_ID_USUARIO_CANCELAMENTO)
    begin
       if (select count(*)
           from   USR_USUARIO t1, inserted t2
           where  t1.USR_ID_USUARIO = t2.LCC_ID_USUARIO_CANCELAMENTO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "USR_USUARIO". Cannot create child in "LCC_LANCAMENTO_CANCELADO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_LCC_LANCAMENTO_CANCELADO on LCC_LANCAMENTO_CANCELADO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "CNB_CONTA_BANCARIA" must exist when updating a child in "LCC_LANCAMENTO_CANCELADO"  */
      if update(LCC_ID_CONTA)
      begin
         if (select count(*)
             from   CNB_CONTA_BANCARIA t1, inserted t2
             where  t1.CNB_ID_CONTA = t2.LCC_ID_CONTA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CNB_CONTA_BANCARIA" does not exist. Cannot modify child in "LCC_LANCAMENTO_CANCELADO".'
               goto error
            end
      end
      /*  Parent "USR_USUARIO" must exist when updating a child in "LCC_LANCAMENTO_CANCELADO"  */
      if update(LCC_ID_USUARIO_LANCAMENTO)
      begin
         if (select count(*)
             from   USR_USUARIO t1, inserted t2
             where  t1.USR_ID_USUARIO = t2.LCC_ID_USUARIO_LANCAMENTO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'USR_USUARIO" does not exist. Cannot modify child in "LCC_LANCAMENTO_CANCELADO".'
               goto error
            end
      end
      /*  Parent "USR_USUARIO" must exist when updating a child in "LCC_LANCAMENTO_CANCELADO"  */
      if update(LCC_ID_USUARIO_CANCELAMENTO)
      begin
         if (select count(*)
             from   USR_USUARIO t1, inserted t2
             where  t1.USR_ID_USUARIO = t2.LCC_ID_USUARIO_CANCELAMENTO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'USR_USUARIO" does not exist. Cannot modify child in "LCC_LANCAMENTO_CANCELADO".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go



create trigger TU_LNC_LANCAMENTO_FINANCEIRO on LNC_LANCAMENTO_FINANCEIRO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "CNB_CONTA_BANCARIA" must exist when updating a child in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(LNC_ID_CONTA)
      begin
         if (select count(*)
             from   CNB_CONTA_BANCARIA t1, inserted t2
             where  t1.CNB_ID_CONTA = t2.LNC_ID_CONTA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CNB_CONTA_BANCARIA" does not exist. Cannot modify child in "LNC_LANCAMENTO_FINANCEIRO".'
               goto error
            end
      end
      /*  Parent "MOV_MOVIMENTACAO_FINANCEIRA" must exist when updating a child in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(LNC_ID_MOVIMENTACAO)
      begin
         if (select count(*)
             from   MOV_MOVIMENTACAO_FINANCEIRA t1, inserted t2
             where  t1.MOV_ID_MOVIMENTACAO = t2.LNC_ID_MOVIMENTACAO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'MOV_MOVIMENTACAO_FINANCEIRA" does not exist. Cannot modify child in "LNC_LANCAMENTO_FINANCEIRO".'
               goto error
            end
      end
      /*  Parent "TTF_TITULO_FINANCEIRO" must exist when updating a child in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(LNC_ID_TITULO)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  LNC_ID_TITULO is null)
         if @numnull != @numrows
            if (select count(*)
                from   TTF_TITULO_FINANCEIRO t1, inserted t2
                where  t1.TTF_ID_TITULO = t2.LNC_ID_TITULO) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'TTF_TITULO_FINANCEIRO" does not exist. Cannot modify child in "LNC_LANCAMENTO_FINANCEIRO".'
               goto error
            end
      end
      /*  Parent "REC_REPASSE_CLIENTE" must exist when updating a child in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(LNC_ID_REPASSE_CLIENTE)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  LNC_ID_REPASSE_CLIENTE is null)
         if @numnull != @numrows
            if (select count(*)
                from   REC_REPASSE_CLIENTE t1, inserted t2
                where  t1.REC_ID_REPASSE_CLIENTE = t2.LNC_ID_REPASSE_CLIENTE) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'REC_REPASSE_CLIENTE" does not exist. Cannot modify child in "LNC_LANCAMENTO_FINANCEIRO".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go



create trigger TD_CLI_CLIENTE3 on PEF_PESSOA_FISICA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_PEF_PESSOA_FISICA on PEF_PESSOA_FISICA for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "PEF_PESSOA_FISICA"  */
    if update(PEF_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.PEF_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "PEF_PESSOA_FISICA".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CLI_CLIENTE3 on PEF_PESSOA_FISICA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CLI_CLIENTE4 on PEJ_PESSOA_JURIDICA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_PEJ_PESSOA_JURIDICA on PEJ_PESSOA_JURIDICA for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "PEJ_PESSOA_JURIDICA"  */
    if update(PEJ_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.PEJ_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "PEJ_PESSOA_JURIDICA".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CLI_CLIENTE4 on PEJ_PESSOA_JURIDICA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_CLI_CLIENTE2 on PES_PESSOA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "USR_USUARIO"  */
    if exists (select 1
               from   USR_USUARIO t2, deleted t1
               where  t2.USR_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "USR_USUARIO". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "END_ENDERECO"  */
    if exists (select 1
               from   END_ENDERECO t2, deleted t1
               where  t2.END_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "END_ENDERECO". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "TEL_TELEFONE"  */
    if exists (select 1
               from   TEL_TELEFONE t2, deleted t1
               where  t2.TEL_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "TEL_TELEFONE". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "CLI_CLIENTE"  */
    if exists (select 1
               from   CLI_CLIENTE t2, deleted t1
               where  t2.CLI_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "CLI_CLIENTE". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "PEF_PESSOA_FISICA"  */
    if exists (select 1
               from   PEF_PESSOA_FISICA t2, deleted t1
               where  t2.PEF_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "PEF_PESSOA_FISICA". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "PEJ_PESSOA_JURIDICA"  */
    if exists (select 1
               from   PEJ_PESSOA_JURIDICA t2, deleted t1
               where  t2.PEJ_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "PEJ_PESSOA_JURIDICA". Cannot delete parent "PES_PESSOA".'
          goto error
       end

    /*  Cannot delete parent "PES_PESSOA" if children still exist in "SAC_SACADO"  */
    if exists (select 1
               from   SAC_SACADO t2, deleted t1
               where  t2.SAC_ID_PESSOA = t1.PES_ID_PESSOA)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "SAC_SACADO". Cannot delete parent "PES_PESSOA".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_CLI_CLIENTE2 on PES_PESSOA for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "USR_USUARIO"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   USR_USUARIO t2, inserted i1, deleted d1
                    where  t2.USR_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "USR_USUARIO". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "END_ENDERECO"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   END_ENDERECO t2, inserted i1, deleted d1
                    where  t2.END_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "END_ENDERECO". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "TEL_TELEFONE"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   TEL_TELEFONE t2, inserted i1, deleted d1
                    where  t2.TEL_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "TEL_TELEFONE". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "CLI_CLIENTE"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   CLI_CLIENTE t2, inserted i1, deleted d1
                    where  t2.CLI_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "CLI_CLIENTE". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "PEF_PESSOA_FISICA"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   PEF_PESSOA_FISICA t2, inserted i1, deleted d1
                    where  t2.PEF_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "PEF_PESSOA_FISICA". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "PEJ_PESSOA_JURIDICA"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   PEJ_PESSOA_JURIDICA t2, inserted i1, deleted d1
                    where  t2.PEJ_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "PEJ_PESSOA_JURIDICA". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end

      /*  Cannot modify parent code in "PES_PESSOA" if children still exist in "SAC_SACADO"  */
      if update(PES_ID_PESSOA)
      begin
         if exists (select 1
                    from   SAC_SACADO t2, inserted i1, deleted d1
                    where  t2.SAC_ID_PESSOA = d1.PES_ID_PESSOA
                     and  (i1.PES_ID_PESSOA != d1.PES_ID_PESSOA))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "SAC_SACADO". Cannot modify parent code in "PES_PESSOA".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_SAC_SACADO on SAC_SACADO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "SAC_SACADO"  */
    if update(SAC_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.SAC_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "SAC_SACADO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_SAC_SACADO on SAC_SACADO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "PES_PESSOA" must exist when updating a child in "SAC_SACADO"  */
      if update(SAC_ID_PESSOA)
      begin
         if (select count(*)
             from   PES_PESSOA t1, inserted t2
             where  t1.PES_ID_PESSOA = t2.SAC_ID_PESSOA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'PES_PESSOA" does not exist. Cannot modify child in "SAC_SACADO".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_TEL_TELEFONE on TEL_TELEFONE for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "TEL_TELEFONE"  */
    if update(TEL_ID_PESSOA)
    begin
       if (select count(*)
           from   PES_PESSOA t1, inserted t2
           where  t1.PES_ID_PESSOA = t2.TEL_ID_PESSOA) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "TEL_TELEFONE".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_TEL_TELEFONE on TEL_TELEFONE for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "PES_PESSOA" must exist when updating a child in "TEL_TELEFONE"  */
      if update(TEL_ID_PESSOA)
      begin
         if (select count(*)
             from   PES_PESSOA t1, inserted t2
             where  t1.PES_ID_PESSOA = t2.TEL_ID_PESSOA) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'PES_PESSOA" does not exist. Cannot modify child in "TEL_TELEFONE".'
               goto error
            end
      end

      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_TTF_TITULO_FINANCEIRO on TTF_TITULO_FINANCEIRO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "TTF_TITULO_FINANCEIRO" if children still exist in "LNC_LANCAMENTO_FINANCEIRO"  */
    if exists (select 1
               from   LNC_LANCAMENTO_FINANCEIRO t2, deleted t1
               where  t2.LNC_ID_TITULO = t1.TTF_ID_TITULO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "LNC_LANCAMENTO_FINANCEIRO". Cannot delete parent "TTF_TITULO_FINANCEIRO".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_TTF_TITULO_FINANCEIRO on TTF_TITULO_FINANCEIRO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "CVN_CONVENIO" must exist when inserting a child in "TTF_TITULO_FINANCEIRO"  */
    if update(TTF_ID_CONVENIO)
    begin
       if (select count(*)
           from   CVN_CONVENIO t1, inserted t2
           where  t1.CVN_ID_CONVENIO = t2.TTF_ID_CONVENIO) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CVN_CONVENIO". Cannot create child in "TTF_TITULO_FINANCEIRO".'
             goto error
          end
    end
    /*  Parent "CLI_CLIENTE" must exist when inserting a child in "TTF_TITULO_FINANCEIRO"  */
    if update(TTF_ID_CLIENTE)
    begin
       if (select count(*)
           from   CLI_CLIENTE t1, inserted t2
           where  t1.CLI_ID_CLIENTE = t2.TTF_ID_CLIENTE) != @numrows
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CLI_CLIENTE". Cannot create child in "TTF_TITULO_FINANCEIRO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_TTF_TITULO_FINANCEIRO on TTF_TITULO_FINANCEIRO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "CVN_CONVENIO" must exist when updating a child in "TTF_TITULO_FINANCEIRO"  */
      if update(TTF_ID_CONVENIO)
      begin
         if (select count(*)
             from   CVN_CONVENIO t1, inserted t2
             where  t1.CVN_ID_CONVENIO = t2.TTF_ID_CONVENIO) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CVN_CONVENIO" does not exist. Cannot modify child in "TTF_TITULO_FINANCEIRO".'
               goto error
            end
      end
      /*  Parent "CLI_CLIENTE" must exist when updating a child in "TTF_TITULO_FINANCEIRO"  */
      if update(TTF_ID_CLIENTE)
      begin
         if (select count(*)
             from   CLI_CLIENTE t1, inserted t2
             where  t1.CLI_ID_CLIENTE = t2.TTF_ID_CLIENTE) != @numrows
            begin
               select @errno  = 50003,
                      @errmsg = 'CLI_CLIENTE" does not exist. Cannot modify child in "TTF_TITULO_FINANCEIRO".'
               goto error
            end
      end
      /*  Cannot modify parent code in "TTF_TITULO_FINANCEIRO" if children still exist in "LNC_LANCAMENTO_FINANCEIRO"  */
      if update(TTF_ID_TITULO)
      begin
         if exists (select 1
                    from   LNC_LANCAMENTO_FINANCEIRO t2, inserted i1, deleted d1
                    where  t2.LNC_ID_TITULO = d1.TTF_ID_TITULO
                     and  (i1.TTF_ID_TITULO != d1.TTF_ID_TITULO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "LNC_LANCAMENTO_FINANCEIRO". Cannot modify parent code in "TTF_TITULO_FINANCEIRO".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TD_USR_USUARIO on USR_USUARIO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Cannot delete parent "USR_USUARIO" if children still exist in "ACU_ACESSO_USUARIO"  */
    if exists (select 1
               from   ACU_ACESSO_USUARIO t2, deleted t1
               where  t2.ACU_ID_USUARIO = t1.USR_ID_USUARIO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "ACU_ACESSO_USUARIO". Cannot delete parent "USR_USUARIO".'
          goto error
       end

    /*  Cannot delete parent "USR_USUARIO" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
    if exists (select 1
               from   LCC_LANCAMENTO_CANCELADO t2, deleted t1
               where  t2.LCC_ID_USUARIO_LANCAMENTO = t1.USR_ID_USUARIO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot delete parent "USR_USUARIO".'
          goto error
       end

    /*  Cannot delete parent "USR_USUARIO" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
    if exists (select 1
               from   LCC_LANCAMENTO_CANCELADO t2, deleted t1
               where  t2.LCC_ID_USUARIO_CANCELAMENTO = t1.USR_ID_USUARIO)
       begin
          select @errno  = 50006,
                 @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot delete parent "USR_USUARIO".'
          goto error
       end


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TI_USR_USUARIO on USR_USUARIO for insert as
begin
    declare
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    /*  Parent "PES_PESSOA" must exist when inserting a child in "USR_USUARIO"  */
    if update(USR_ID_PESSOA)
    begin
       select @numnull = (select count(*)
                          from   inserted
                          where  USR_ID_PESSOA is null)
       if @numnull != @numrows
          if (select count(*)
              from   PES_PESSOA t1, inserted t2
              where  t1.PES_ID_PESSOA = t2.USR_ID_PESSOA) != @numrows - @numnull
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "PES_PESSOA". Cannot create child in "USR_USUARIO".'
             goto error
          end
    end
    /*  Parent "CLI_CLIENTE" must exist when inserting a child in "USR_USUARIO"  */
    if update(USR_ID_CLIENTE)
    begin
       select @numnull = (select count(*)
                          from   inserted
                          where  USR_ID_CLIENTE is null)
       if @numnull != @numrows
          if (select count(*)
              from   CLI_CLIENTE t1, inserted t2
              where  t1.CLI_ID_CLIENTE = t2.USR_ID_CLIENTE) != @numrows - @numnull
          begin
             select @errno  = 50002,
                    @errmsg = 'Parent does not exist in "CLI_CLIENTE". Cannot create child in "USR_USUARIO".'
             goto error
          end
    end

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TU_USR_USUARIO on USR_USUARIO for update as
begin
   declare
      @numrows  int,
      @numnull  int,
      @errno    int,
      @errmsg   varchar(255)

      select  @numrows = @@rowcount
      if @numrows = 0
         return

      /*  Parent "PES_PESSOA" must exist when updating a child in "USR_USUARIO"  */
      if update(USR_ID_PESSOA)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  USR_ID_PESSOA is null)
         if @numnull != @numrows
            if (select count(*)
                from   PES_PESSOA t1, inserted t2
                where  t1.PES_ID_PESSOA = t2.USR_ID_PESSOA) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'PES_PESSOA" does not exist. Cannot modify child in "USR_USUARIO".'
               goto error
            end
      end
      /*  Parent "CLI_CLIENTE" must exist when updating a child in "USR_USUARIO"  */
      if update(USR_ID_CLIENTE)
      begin
         select @numnull = (select count(*)
                            from   inserted
                            where  USR_ID_CLIENTE is null)
         if @numnull != @numrows
            if (select count(*)
                from   CLI_CLIENTE t1, inserted t2
                where  t1.CLI_ID_CLIENTE = t2.USR_ID_CLIENTE) != @numrows - @numnull
            begin
               select @errno  = 50003,
                      @errmsg = 'CLI_CLIENTE" does not exist. Cannot modify child in "USR_USUARIO".'
               goto error
            end
      end
      /*  Cannot modify parent code in "USR_USUARIO" if children still exist in "ACU_ACESSO_USUARIO"  */
      if update(USR_ID_USUARIO)
      begin
         if exists (select 1
                    from   ACU_ACESSO_USUARIO t2, inserted i1, deleted d1
                    where  t2.ACU_ID_USUARIO = d1.USR_ID_USUARIO
                     and  (i1.USR_ID_USUARIO != d1.USR_ID_USUARIO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "ACU_ACESSO_USUARIO". Cannot modify parent code in "USR_USUARIO".'
               goto error
            end
      end

      /*  Cannot modify parent code in "USR_USUARIO" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
      if update(USR_ID_USUARIO)
      begin
         if exists (select 1
                    from   LCC_LANCAMENTO_CANCELADO t2, inserted i1, deleted d1
                    where  t2.LCC_ID_USUARIO_LANCAMENTO = d1.USR_ID_USUARIO
                     and  (i1.USR_ID_USUARIO != d1.USR_ID_USUARIO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot modify parent code in "USR_USUARIO".'
               goto error
            end
      end

      /*  Cannot modify parent code in "USR_USUARIO" if children still exist in "LCC_LANCAMENTO_CANCELADO"  */
      if update(USR_ID_USUARIO)
      begin
         if exists (select 1
                    from   LCC_LANCAMENTO_CANCELADO t2, inserted i1, deleted d1
                    where  t2.LCC_ID_USUARIO_CANCELAMENTO = d1.USR_ID_USUARIO
                     and  (i1.USR_ID_USUARIO != d1.USR_ID_USUARIO))
            begin
               select @errno  = 50005,
                      @errmsg = 'Children still exist in "LCC_LANCAMENTO_CANCELADO". Cannot modify parent code in "USR_USUARIO".'
               goto error
            end
      end


      return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go
